package com.maiyuren.dict_page_view_plugin.dictpageview;


import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.ValueCallback;
import android.webkit.WebView;

import com.maiyuren.dict_page_view_plugin.DictPageViewPlugin;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by 45524 on 2018/2/10.
 */

public class DictActionWebView extends WebView {

    ActionMode mActionMode;
    int _viewId;
    int _tabIndex;

    public DictActionWebView(Context context, int viewId, int tabIndex) {
        super(context);
        _viewId = viewId;
        _tabIndex = tabIndex;
    }

    public DictActionWebView(Context context, AttributeSet as) {
        super(context, as);
    }

    @Override
    public ActionMode startActionMode(ActionMode.Callback callback) {
        ActionMode actionMode = super.startActionMode(callback);
        return resolveActionMode(actionMode);
    }
    @Override
    public ActionMode startActionMode(ActionMode.Callback callback, int type) {
        ActionMode actionMode = super.startActionMode(callback, type);
        return resolveActionMode(actionMode);
    }
    /**
     * 处理item，处理点击
     * @param actionMode
     */
    private ActionMode resolveActionMode(ActionMode actionMode) {
        if (actionMode != null) {
            final Menu menu = actionMode.getMenu();
            mActionMode = actionMode;
            menu.clear();
            menu.add("拷貝");
            menu.add("全選");
            menu.add("搜尋");

            for (int i = 0; i < menu.size(); i++) {
                final MenuItem menuItem = menu.getItem(i);

                menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        if (item.getTitle() == "全選") {
                            selectAll();
                        } else {
                            if (item.getTitle() == "拷貝") {
                                getSelectedData((value) -> {
                                    ClipboardManager mClipboard = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                                    // Set the clipboard's primary clip.
                                    ClipData data = ClipData.newPlainText("text", value);

                                    mClipboard.setPrimaryClip(data);
                                    mActionMode.finish();
                                    mActionMode = null;
                                });
                            }
                            else if (item.getTitle() == "搜尋") {
                                getSelectedData((value) -> {
                                    Map<String, Object> param = new HashMap<>();
                                    param.put("id", _viewId);
                                    param.put("tabIndex", _tabIndex);
                                    param.put("word", value);
                                    DictPageViewPlugin.channel.invokeMethod("search", utils.map2string(param));

                                    mActionMode.finish();
                                    mActionMode = null;
                                });
                            }
                        }


                        return true;
                    }
                });
            }
        }


        mActionMode = actionMode;
        return actionMode;
    }

    private void getSelectedData(ValueCallback<String> resultCallback) {
        evaluateJavascript("window.getSelection().toString();", (value)->{
            value = value.substring(1, value.length() - 1);
            value = value.replaceAll("\\\\n", "\n");
            resultCallback.onReceiveValue(value);
        } );
    }

    private void selectAll() {
        evaluateJavascript("document.execCommand(\"selectAll\");", null);
    }
}
